package com.aizhixin.test.common.uitl;


import java.util.HashMap;
import java.util.Random;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.context.support.StaticApplicationContext;

import com.baidu.aip.face.AipFace;






/**  
 * <p>人脸识别</p> 
 */
public class Sample {

	//APPID
    public static final String APP_ID = "16358144";
    //API_KEY
    public static final String API_KEY = "WwDcOaBYlsc52vepZMSnV9ub";
    //SECRET_KEY
    public static final String SECRET_KEY = "s4cXuQSpVG59jWl5iWedH2LUFo0mjTHq";
    //client  AipFace对象
    private static AipFace client = null;

    
    static {
    	getAipFace();
    }
    
    /**
     *<p>获取 AipFace对象</p> 
     * @Title: getAipFace    
     * @version:V0.1     
     * @return    
     * @return:AipFace
     */
    public static AipFace getAipFace() {
    	client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
    	client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
    	return client;
    }
    
    
    /**
     *<p>人脸注册</p> 
     * @Title: faceRe    
     * @version:V0.1     
     * @param imageBase64
     * @return    
     * @return:String
     */
    public static String faceRe(String imageBase64) {
    	// 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("user_info", "user's info");
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");
        
        String imageType = "BASE64";
        String groupId = "01";
        String userId = getStringRandom(12);
        
        // 人脸注册
        JSONObject res = client.addUser(imageBase64, imageType, groupId, userId, options);
        /**
         	* 直接获取的值face_token
         */
        System.out.println(res.toString(2));
        JSONObject result;
		String[] opedId;
		/*
		 * try catch 如果不知道没有这个
		 * 尝试try里面的，失败就执行下面这个catch里面的知道什么错误，如果前端收不到数据就会凉凉
		 */
		try {
			result = res.getJSONObject("result");
			opedId = JSONObject.getNames(result);
		} catch (JSONException e) {
			e.printStackTrace();
			return "fail";//如果没有这一步，直接返回空，前端就不知道是照片的错，就会报错无法执行（微信小程序的问题会报400）
		}
        
        return (String) result.get(opedId[0]);

    }
//生成随机用户名，数字和字母组成,  
     public static  String getStringRandom(int length) { 
	String val = "";
         Random random = new Random();  //参数length，表示生成几位随机数  
         for(int i = 0; i < length; i++) {
             String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";  //输出字母还是数字  
             if( "char".equalsIgnoreCase(charOrNum) ) {   //输出是大写字母还是小写字母  
                 int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
                 val += (char)(random.nextInt(26) + temp);  
             } else if( "num".equalsIgnoreCase(charOrNum) ) {  
                 val += String.valueOf(random.nextInt(10));  
             }  
         }  
         return val;  
     }

    
    /**
     *<p>人脸检测</p> 
     * @Title: searchFace    
     * @version:V0.1     
     * @param imageBase64
     * @return    
     * @return:String
     */
    public static String searchFace(String imageBase64) {
    	// 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "NONE");
        
        String image = imageBase64;
        String imageType = "BASE64";
        String groupIdList = "01";
        // 人脸搜索
    	try {
        JSONObject res = client.search(image, imageType, groupIdList, options);
        System.out.println(res.toString(2));
        return res.toString(2);
    	} catch (JSONException e)
    	{e.printStackTrace();
		return "fail人脸检测失败";}
    	
        /*
         * 	try {
			result = res.getJSONObject("result");
			opedId = JSONObject.getNames(result);
		} catch (JSONException e) {
			e.printStackTrace();
			return "fail";//如果没有这一步，直接返回空，前端就不知道是照片的错，就会报错无法执行（微信小程序的问题会报400）
		}
         */
    }
  

    

    
}
